{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "fef099c1-3b7c-41aa-b96b-7e3fe02ca836",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "33273597-84e5-42a7-8ffd-710d524b57f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取图片\n",
    "img = cv2.imread('test1.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "09f44eac-f6fb-421a-942d-e31af3f117c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 灰度化\n",
    "gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "aee74f2d-48af-45d9-9076-ce09335b909f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建ORB对象\n",
    "orb = cv2.ORB_create()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5c22e68c-c02e-455f-b00a-79504622c12d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "keyPoint[0] \n",
      "< cv2.KeyPoint 000002CF6C29BF90>\n",
      "description[0] \n",
      "[120 185 152 248 169 237  81 199 171  61 172   9 175 191  66  26 129 187\n",
      " 253  16  30  75 210 177 219 213  20 123 210  64 142  27]\n",
      "keyPoints num 500\n",
      "description dimension 32\n",
      "description num 500.0\n",
      "description dimension * num = 16000\n"
     ]
    }
   ],
   "source": [
    "# 进行检测\n",
    "'''\n",
    "kp = orb.detect(gray, None)\n",
    "\n",
    "print(kp)\n",
    "'''\n",
    "# 相较于detect，detectAndCompute除了返回特征点，还会返回描述子\n",
    "kp, des = orb.detectAndCompute(gray, None)\n",
    "\n",
    "print('keyPoint[0] \\n{}'.format(kp[0]))\n",
    "print('description[0] \\n{}'.format(des[0]))\n",
    "\n",
    "print('keyPoints num {}'.format(len(kp)))\n",
    "\n",
    "print('description dimension {}'.format(des[0].size))\n",
    "print('description num {}'.format(des.size/des[0].size))\n",
    "print('description dimension * num = {}'.format(des.size))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "69673c66-b1d0-4f83-9918-a9e1a48d2f6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 绘制keypoints\n",
    "cv2.drawKeypoints(gray, kp, img)\n",
    "\n",
    "cv2.imshow('orb', img)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1729404f-a114-4a91-b253-d80364f578ca",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
